home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gpp-1_42.lha / g++-1.42.0 / cplus-except.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  31KB  |  1,027 lines

  1. /* Handle exceptional things in C++.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@mcc.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #define NULL 0
  25. #define EXCEPTION_NAME_PREFIX "__exception_"
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "cplus-tree.h"
  30. #include "flags.h"
  31. #include "assert.h"
  32. /* On Suns this can get you to the right definition if you
  33.    set the right value for TARGET.  */
  34. #include <setjmp.h>
  35. #ifdef sequent
  36. /* Can you believe they forgot this?  */
  37. #define _JBLEN 11
  38. #endif
  39.  
  40. #ifndef _JBLEN
  41. #define _JBLEN (sizeof(jmp_buf)/sizeof(int))
  42. #endif
  43.  
  44. void init_exception_processing ();
  45. void init_exception_processing_1 ();
  46.  
  47. /* If non-zero, a VAR_DECL whose cleanup will cause a throw to the
  48.    next exception handler.  Its value says whether to throw or not.
  49.    In the case of functions which do not issue a RAISE, it should be
  50.    possible to optimize away this VAR_DECL (and overhead associated
  51.    with it).  */
  52. tree exception_throw_decl;
  53. /* Use this to know that we did not set `exception_throw_decl',
  54.    until GCC optimizer is smart enough to figure it out for itself.  */
  55. int sets_exception_throw_decl;
  56.  
  57. /* The exception `type' currently in scope, or NULL_TREE if none.  */
  58. tree current_exception_type;
  59.  
  60. /* The exception handler object for the given scope.  */
  61. tree current_exception_decl;
  62.  
  63. /* The ``object'' view of the current exception parameters.
  64.    We cast up from the `parms' field to `current_exception_type'.  */
  65. tree current_exception_object;
  66.  
  67. /* Low-level rtl interface.  */
  68. #include "rtl.h"
  69.  
  70. /* Cache `setjmp', `longjmp', `raise_exception', and `unhandled_exception'
  71.    after default conversion.  Maybe later they will get built-in.  */
  72. static tree BISJ, BILJ, BIR, BIUE;
  73.  
  74. /* Local variables which give the appearance that exception
  75.    handling is part of the language and the execution model.  */
  76.  
  77. /* The type of the exception handler stack.  */
  78. static tree EHS_type;
  79.  
  80. /* The global handler stack.  */
  81. tree EHS_decl;
  82.  
  83. /* Cached component refs to fields of `EHS_decl'.  */
  84. static tree EHS_prev, EHS_handler, EHS_parms, EHS_name;
  85.  
  86. /* The parameter names of this exception type.  */
  87.  
  88. static tree last_exception_fields;
  89. static tree last_exception_field_types;
  90.  
  91. /* When ID is VOID_TYPE_NODE, it means ``raise all''.
  92.    Cannot be inline, since it uses `alloca', and that
  93.    breaks code which pushes the result of this function
  94.    on the stack.  */
  95. static tree
  96. exception_object_name (prefix, id)
  97.      tree prefix;
  98.      tree id;
  99. {
  100.   /* First, cons up the `name' of this exception.  */
  101.   char *name;
  102.   int length = (id == void_type_node ? 3 : IDENTIFIER_LENGTH (id)) + EXCEPTION_NAME_LENGTH;
  103.  
  104.   if (prefix)
  105.     length += IDENTIFIER_LENGTH (prefix) + 2;
  106.  
  107.   name = (char *)alloca (length);
  108.   strcpy (name, EXCEPTION_NAME_PREFIX);
  109.   length = EXCEPTION_NAME_LENGTH;
  110.   if (prefix)
  111.     {
  112.       strcpy (name + length, IDENTIFIER_POINTER (prefix));
  113.       name[length + IDENTIFIER_LENGTH (prefix)] = JOINER;
  114.       length += IDENTIFIER_LENGTH (prefix) + 1;
  115.     }
  116.   if (id == void_type_node)
  117.     strcpy (name + length, "all");
  118.   else
  119.     strcpy (name + length, IDENTIFIER_POINTER (id));
  120.   return get_identifier (name);
  121. }
  122.  
  123. tree
  124. lookup_exception_cname (ctype, cname, raise_id)
  125.      tree ctype, cname;
  126.      tree raise_id;
  127. {
  128.   tree this_cname = TREE_PURPOSE (raise_id);
  129.   if (this_cname == NULL_TREE)
  130.     {
  131.       if (cname)
  132.     {
  133.       tree name = TREE_VALUE (raise_id);
  134.       if (purpose_member (name, CLASSTYPE_TAGS (ctype)))
  135.         this_cname = cname;
  136.     }
  137.     }
  138.   else if (this_cname == void_type_node)
  139.     this_cname = NULL_TREE;
  140.   else if (TREE_CODE (this_cname) != IDENTIFIER_NODE)
  141.     {
  142.       sorry ("multiple scope refs in `cplus_expand_raise_stmt'");
  143.       this_cname = error_mark_node;
  144.     }
  145.   return this_cname;
  146. }
  147.  
  148. tree
  149. lookup_exception_tname (oname)
  150.      tree oname;
  151. {
  152.   return get_identifier (IDENTIFIER_POINTER (oname) + EXCEPTION_NAME_LENGTH);
  153. }
  154.  
  155. tree
  156. lookup_exception_object (cname, name, complain)
  157.      tree cname, name;
  158.      int complain;
  159. {
  160.   tree oname;
  161.   tree decl;
  162.  
  163.   if (cname == void_type_node)
  164.     cname = NULL_TREE;
  165.   else if (cname && TREE_CODE (cname) != IDENTIFIER_NODE)
  166.     {
  167.       sorry ("multiple scope refs in `lookup_exception_object'");
  168.       cname = NULL_TREE;
  169.     }
  170.   oname = exception_object_name (cname, name);
  171.   decl = IDENTIFIER_GLOBAL_VALUE (oname);
  172.   if (decl == NULL_TREE || TREE_CODE (decl) != VAR_DECL)
  173.     {
  174.       if (complain)
  175.     {
  176.       int temp = allocation_temporary_p ();
  177.       if (cname)
  178.         error ("no exception name object for name `%s::%s'",
  179.            IDENTIFIER_POINTER (cname),
  180.            IDENTIFIER_POINTER (name));
  181.       else
  182.         error ("no exception name object for name `%s'",
  183.            IDENTIFIER_POINTER (name));
  184.       if (temp)
  185.         end_temporary_allocation ();
  186.       /* Avoid further error messages.  */
  187.       pushdecl_top_level (build_lang_field_decl (VAR_DECL,
  188.                              exception_object_name (cname, name),
  189.                              error_mark_node));
  190.       if (temp)
  191.         resume_temporary_allocation ();
  192.     }
  193.       return NULL_TREE;
  194.     }
  195.   return decl;
  196. }
  197.  
  198. tree
  199. lookup_exception_type (ctype, cname, raise_id)
  200.      tree ctype, cname;
  201.      tree raise_id;
  202. {
  203.   tree name = TREE_VALUE (raise_id);
  204.   tree purpose = TREE_PURPOSE (raise_id);
  205.  
  206.   if (cname && purpose == NULL_TREE)
  207.     purpose = cname;
  208.  
  209.   if (purpose && purpose != void_type_node)
  210.     {
  211.       tree assoc = NULL_TREE;
  212.  
  213.       if (TREE_CODE (purpose) != IDENTIFIER_NODE)
  214.     {
  215.       sorry ("multiple scope refs in `lookup_exception_type'");
  216.       TREE_PURPOSE (raise_id) = NULL_TREE;
  217.       return NULL_TREE;
  218.     }
  219.       if (! is_aggr_typedef (purpose, 1))
  220.     return NULL_TREE;
  221.       ctype = TREE_TYPE (TREE_TYPE (purpose));
  222.       assoc = purpose_member (name, CLASSTYPE_TAGS (ctype));
  223.       if (assoc)
  224.     return TREE_VALUE (assoc);
  225.     }
  226.  
  227.   ctype = lookup_name (name);
  228.   if (ctype && TREE_CODE (ctype) == TYPE_DECL)
  229.     ctype = TREE_TYPE (ctype);
  230.   if (ctype && TREE_CODE (ctype) == RECORD_TYPE
  231.       && CLASSTYPE_DECLARED_EXCEPTION (ctype))
  232.     return ctype;
  233.   return NULL_TREE;
  234. }
  235.  
  236. tree
  237. finish_exception (e, list_of_fieldlists)
  238.      tree e;
  239.      tree list_of_fieldlists;
  240. {
  241.   tree parmtypes = NULL_TREE, name_field;
  242.   tree cname = TYPE_NAME (e);
  243.  
  244.   if (TREE_CODE (cname) == TYPE_DECL)
  245.     cname = DECL_NAME (cname);
  246.  
  247.   if (last_exception_fields)
  248.     error ("cannot declare exceptions within exceptions");
  249.   if (list_of_fieldlists && ! ANON_AGGRNAME_P (cname))
  250.     error_with_aggr_type (e, "exception name `%s' must follow body declaration");
  251.   if (list_of_fieldlists)
  252.     {
  253.       tree prev, field;
  254.  
  255.       /* Note: no public, private, or protected allowed.  */
  256.       if (TREE_CHAIN (list_of_fieldlists))
  257.     error ("visibility declarations invalid in exception declaration");
  258.       else if (TREE_PURPOSE (list_of_fieldlists) != (tree)visibility_default)
  259.     error ("visibility declarations invalid in exception declaration");
  260.       TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_default;
  261.  
  262.       /* Note also: no member function declarations allowed.  */
  263.       for (prev = 0, field = TREE_VALUE (list_of_fieldlists);
  264.        field; prev = field, field = TREE_CHAIN (field))
  265.     {
  266.       switch (TREE_CODE (field))
  267.         {
  268.         case FIELD_DECL:
  269.           /* ok.  */
  270.           parmtypes = tree_cons (NULL_TREE, TREE_TYPE (field), parmtypes);
  271.           continue;
  272.         case FUNCTION_DECL:
  273.           error_with_decl (field, "declaration of function `%s' in exception invalid");
  274.           break;
  275.         case VAR_DECL:
  276.           if (TREE_STATIC (field))
  277.         error_with_decl (field, "declaration of static variable `%s' in exception invalid");
  278.           else
  279.         error_with_decl (field, "declaration of constant field `%s' in exception invalid");
  280.           break;
  281.         case CONST_DECL:
  282.           error_with_decl (field, "declaration of enum value `%s' in exception invalid");
  283.           break;
  284.         case SCOPE_REF:
  285.           error ("use of `::' in exception context invalid");
  286.           break;
  287.         }
  288.       if (prev)
  289.         TREE_CHAIN (prev) = TREE_CHAIN (field);
  290.       else
  291.         TREE_VALUE (list_of_fieldlists) = TREE_CHAIN (field);
  292.     }
  293.     }
  294.  
  295.   /* Now that we've cleaned up the fields, add a name identifier at front.  */
  296.   name_field = build_lang_field_decl (FIELD_DECL, get_identifier ("__name"),
  297.                       ptr_type_node);
  298.   if (list_of_fieldlists)
  299.     {
  300.       TREE_CHAIN (name_field) = TREE_VALUE (list_of_fieldlists);
  301.       TREE_VALUE (list_of_fieldlists) = name_field;
  302.     }
  303.   else
  304.     list_of_fieldlists = build_tree_list (NULL_TREE, name_field);
  305.  
  306.   last_exception_fields = TREE_VALUE (list_of_fieldlists);
  307.   if (parmtypes)
  308.     {
  309.       last_exception_field_types = nreverse (parmtypes);
  310.       /* Set the TREE_CHAIN of what is now at the end of the
  311.      list to `void_list_node'.  */
  312.       TREE_CHAIN (parmtypes) = void_list_node;
  313.     }
  314.   else
  315.     last_exception_field_types = void_list_node;
  316.  
  317.   popclass (0);
  318.  
  319. #if 0
  320.   /* Remove aggregate types from the list of tags,
  321.      since these appear at global scope.  */
  322.   while (x && IS_AGGR_TYPE (TREE_VALUE (x)))
  323.     x = TREE_CHAIN (x);
  324.   CLASSTYPE_TAGS (t) = x;
  325.   y = x;
  326.   while (x)
  327.     {
  328.       if (IS_AGGR_TYPE (TREE_VALUE (x)))
  329.     TREE_CHAIN (y) = TREE_CHAIN (x);
  330.       x = TREE_CHAIN (x);
  331.     }
  332. #endif
  333.  
  334.   return e;
  335. }
  336.  
  337. void
  338. finish_exception_decl (cname, decl)
  339.      tree cname, decl;
  340. {
  341.   /* In cplus-decl.h.  */
  342.   extern tree last_function_parms;
  343.  
  344.   /* An exception declaration.  */
  345.   tree t, ctor;
  346.   tree parmdecls = NULL_TREE, fields;
  347.   tree list_of_fieldlists = temp_tree_cons (NULL_TREE,
  348.                         copy_list (last_exception_fields),
  349.                         NULL_TREE);
  350.   tree edecl = build_lang_field_decl (VAR_DECL,
  351.                       exception_object_name (cname, DECL_NAME (decl)),
  352.                       ptr_type_node);
  353.  
  354.   DECL_LANGUAGE (edecl) = lang_c;
  355.   TREE_STATIC (edecl) = 1;
  356.   TREE_PUBLIC (edecl) = 1;
  357.   finish_decl (pushdecl (edecl), 0, 0);
  358.  
  359.   /* Now instantiate the exception decl.  */
  360.   t = xref_tag (exception_type_node, DECL_NAME (decl), NULL_TREE);
  361.  
  362.   /* finish_struct will pop this.  */
  363.   pushclass (t, 0);
  364.  
  365.   /* Now add a constructor which takes as parameters all the types we
  366.      just defined.  */
  367.   ctor = build_lang_decl (FUNCTION_DECL, DECL_NAME (decl),
  368.               build_cplus_method_type (t, TYPE_POINTER_TO (t),
  369.                            last_exception_field_types));
  370.   /* Don't take `name'.  The constructor handles that.  */
  371.   fields = TREE_CHAIN (TREE_VALUE (list_of_fieldlists));
  372.   while (fields)
  373.     {
  374.       tree parm = build_decl (PARM_DECL, DECL_NAME (fields), TREE_TYPE (fields));
  375.       /* Since there is a prototype, args are passed in their own types.  */
  376.       DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
  377. #ifdef PROMOTE_PROTOTYPES
  378.       if (TREE_CODE (TREE_TYPE (fields)) == INTEGER_TYPE
  379.       && TYPE_PRECISION (TREE_TYPE (fields)) < TYPE_PRECISION (integer_type_node))
  380.     DECL_ARG_TYPE (parm) = integer_type_node;
  381. #endif
  382.       TREE_CHAIN (parm) = parmdecls;
  383.       parmdecls = parm;
  384.       fields = TREE_CHAIN (fields);
  385.     }
  386.   fields = TREE_VALUE (list_of_fieldlists);
  387.   last_function_parms = nreverse (parmdecls);
  388.  
  389.   DECL_CONSTRUCTOR_P (ctor) = 1;
  390.   TYPE_HAS_CONSTRUCTOR (t) = 1;
  391.   grokclassfn (t, DECL_NAME (decl), ctor, NO_SPECIAL, 0, NULL_TREE);
  392.   TREE_EXTERNAL (ctor) = 1;
  393.   TREE_STATIC (ctor) = 1;
  394.   TREE_PUBLIC (ctor) = 0;
  395.   TREE_INLINE (ctor) = 1;
  396.   make_decl_rtl (ctor, 0, 1);
  397.   finish_decl (ctor, NULL_TREE, 0);
  398.   TREE_CHAIN (ctor) = TREE_VALUE (list_of_fieldlists);
  399.   TREE_VALUE (list_of_fieldlists) = ctor;
  400.  
  401.   finish_struct (t, list_of_fieldlists, 0, 0);
  402.  
  403.   if (current_function_decl)
  404.     error ("cannot define exception inside function scope");
  405.   else
  406.     {
  407.       /* Now build the constructor for this exception.  */
  408.       parmdecls = DECL_ARGUMENTS (ctor);
  409.       start_function (NULL_TREE, ctor, 0, 1);
  410.       store_parm_decls ();
  411.       pushlevel (0);
  412.       clear_last_expr ();
  413.       push_momentary ();
  414.       expand_start_bindings (0);
  415.  
  416.       /* Move all the parameters to the fields, skipping `this'.  */
  417.       parmdecls = TREE_CHAIN (parmdecls);
  418.       /* Install `name' of this exception handler.  */
  419.       DECL_INITIAL (fields) = build_unary_op (ADDR_EXPR, edecl, 0);
  420.       fields = TREE_CHAIN (fields);
  421.       /* Install all the values.  */
  422.       while (fields)
  423.     {
  424.       /* Set up the initialization for this field.  */
  425.       DECL_INITIAL (fields) = parmdecls;
  426.       fields = TREE_CHAIN (fields);
  427.       parmdecls = TREE_CHAIN (parmdecls);
  428.     }
  429.       emit_base_init (t, 0);
  430.  
  431.       finish_function (DECL_SOURCE_LINE (ctor), 1);
  432.     }
  433. }
  434.  
  435. void
  436. end_exception_decls ()
  437. {
  438.   last_exception_field_types = NULL_TREE;
  439.   last_exception_fields = NULL_TREE;
  440. }
  441.  
  442. /* Statement-level exception semantics.  */
  443.  
  444. void
  445. cplus_expand_start_try (implicit)
  446.      int implicit;
  447. {
  448.   tree call_to_setjmp;
  449.   tree handler, ref;
  450.  
  451.   /* Start a new block enclosing the whole handler.  */
  452.   if (implicit)
  453.     {
  454.       pushlevel_temporary (1);
  455.     }
  456.   else
  457.     {
  458.       pushlevel (0);
  459.       clear_last_expr ();
  460.       push_momentary ();
  461.  
  462.       /* Encompass whole exception handler in one big binding contour.
  463.      If RAISE should throw out of the whole TRY/EXCEPT block, call
  464.      `expand_start_bindings' with argument of 1.  */
  465.       expand_start_bindings (0);
  466.     }
  467.  
  468.   /* Allocate handler in that block.  It's real name will come later.
  469.      Note that it will be the first name in this binding contour.  */
  470.   handler = get_temp_name (EHS_type, 0);
  471.   DECL_INITIAL (handler) = error_mark_node;
  472.   finish_decl (handler, NULL_TREE, 0);
  473.  
  474.   /* Must come after call to `finish_decl', else the cleanup for the temp
  475.      for the handler will cause the contour we just created to be popped.  */
  476.   if (implicit)
  477.     declare_implicit_exception ();
  478.  
  479.   /* Catch via `setjmp'.  */
  480.   ref = build_component_ref (handler, get_identifier ("handler"), NULL_TREE, 0);
  481.   call_to_setjmp = build_function_call (BISJ, build_tree_list (NULL_TREE, ref));
  482.  
  483.   /* RAISE throws to EXCEPT part.  */
  484.   expand_start_try (build_binary_op (EQ_EXPR, call_to_setjmp, integer_zero_node), 0, 1);
  485. }
  486.  
  487. /* If KEEP is 1, then declarations in the TRY statement are worth keeping.
  488.    If KEEP is 2, then the TRY statement was generated by the compiler.
  489.    If KEEP is 0, the declarations in the TRY statement contain errors.  */
  490.  
  491. tree
  492. cplus_expand_end_try (keep)
  493.      int keep;
  494. {
  495.   tree decls, decl, block;
  496.  
  497.   if (keep < 2)
  498.     pop_implicit_try_blocks (NULL_TREE);
  499.  
  500.   decls = getdecls ();
  501.  
  502.   /* Emit code to avoid falling through into a default
  503.      handler that might come later.  */
  504.   expand_end_try ();
  505.  
  506.   /* Pops binding contour local to TRY, and get the exception handler
  507.      object built by `...start_try'.  */
  508.   switch (keep)
  509.     {
  510.     case 0:
  511.       expand_end_bindings (decls, 0, 1);
  512.       block = poplevel (0, 0, 0);
  513.       pop_momentary (); 
  514.       decl = getdecls ();
  515.       break;
  516.  
  517.     case 1:
  518.       expand_end_bindings (decls, 1, 1);
  519.       block = poplevel (1, 1, 0);
  520.       pop_momentary ();
  521.       decl = getdecls ();
  522.       break;
  523.  
  524.     default:
  525.       decl = tree_last (decls);
  526.       block = NULL_TREE;
  527.       break;
  528.     }
  529.  
  530.   assert (TREE_CODE (decl) == VAR_DECL && TREE_TYPE (decl) == EHS_type);
  531.   if (block)
  532.     {
  533.       TREE_LANG_FLAG_1 (block) = 1;
  534.       TREE_USED (block) = 1;
  535.     }
  536.  
  537.   /* Pass it back so that its rtl can be bound to its name
  538.      (or vice versa).  */
  539.   return decl;
  540. }
  541.  
  542. void
  543. cplus_expand_start_except (name, decl)
  544.      tree name, decl;
  545. {
  546.   int yes;
  547.   tree tmp;
  548.  
  549.   expand_start_except (0, 1);
  550.  
  551.   /* This is internal `eh'.  */
  552.   current_exception_decl = decl;
  553.   /* Get the exception object into scope (user declared `ex').  */
  554.   tmp = pushdecl (build_decl (VAR_DECL, name, ptr_type_node));
  555.   DECL_INITIAL (tmp) = error_mark_node;
  556.   finish_decl (tmp, build (COMPONENT_REF, ptr_type_node, decl, TREE_OPERAND (EHS_parms, 1)), 0);
  557.   current_exception_type = NULL_TREE;
  558.   yes = suspend_momentary ();
  559.   /* From now on, send the user to our faked-up object.  */
  560.   current_exception_object = build1 (INDIRECT_REF, void_type_node, tmp);
  561.   IDENTIFIER_LOCAL_VALUE (name) = current_exception_object;
  562.   resume_momentary (yes);
  563.  
  564.   /* Pop exception handler stack.  */
  565.   expand_assignment (EHS_decl, EHS_prev, 0, 0);
  566. }
  567.  
  568. /* Generate the call to `unhandled_exception' that is appropriate
  569.    for this particular unhandled exception.  */
  570. static tree
  571. call_to_unhandled_exception ()
  572. {
  573.   extern int lineno;
  574.   tree parms = tree_cons (NULL_TREE,
  575.               combine_strings (build_string (strlen (input_filename + 1), input_filename)),
  576.               build_tree_list (NULL_TREE, build_int_2 (lineno, 0)));
  577.   return build_function_call (BIUE, parms);
  578. }
  579.  
  580. /* Note that this must be mirror image of `...start_try'.
  581.    DFAULT is the default clause, if there was one.
  582.    DFAULT is ERROR_MARK_NODE when this ends an implicit handler.  */
  583. void
  584. cplus_expand_end_except (dfault)
  585.      tree dfault;
  586. {
  587.   extern tree expand_end_except (); /* stmt.c.  */
  588.   tree decls, raised;
  589.  
  590.   if (dfault == NULL_TREE)
  591.     {
  592.       /* Uncaught exception at outermost level.  If raised locally,
  593.      reraise the exception.  Otherwise, generate code to call `abort'.  */
  594.       if (in_try_block (1) == 0)
  595.     {
  596.       expand_start_cond (build (EQ_EXPR, integer_type_node,
  597.                     exception_throw_decl, integer_zero_node), 0);
  598.       expand_expr (call_to_unhandled_exception (), 0, VOIDmode, 0);
  599.       expand_end_cond ();
  600.     }
  601.       /* Try the next handler.  */
  602.       if (! expand_escape_except ())
  603.     compiler_error ("except nesting botch");
  604.     }
  605.  
  606.   raised = expand_end_except ();
  607.  
  608.   decls = getdecls ();
  609.   expand_end_bindings (decls, decls != 0, 1);
  610.   poplevel (decls != 0, 1, 0);
  611.  
  612.   /* Implicit handlers do not use the momentary obstack.  */
  613.   if (dfault != error_mark_node)
  614.     pop_momentary ();
  615.  
  616.   if (! in_try_block (1))
  617.     {
  618.       /* Check that this function is not raising exceptions
  619.      it is not supposed to.  */
  620.       while (raised)
  621.     {
  622.       error_with_decl (TREE_VALUE (raised), "exception `%s' raised but not declared raisable");
  623.       raised = TREE_CHAIN (raised);
  624.     }
  625.     }
  626.   else if (dfault == NULL_TREE || dfault == error_mark_node)
  627.     {
  628.       expand_start_cond (build (NE_EXPR, integer_type_node,
  629.                 exception_throw_decl,
  630.                 integer_zero_node), 0);
  631.       /* We fell off the end of this try block.  Try going to the next.
  632.      The escape_label will be the beginning of the next try block.  */
  633.       if (! expand_escape_except ())
  634.     compiler_error ("except nesting botch");
  635.       expand_end_cond ();
  636.     }
  637. }
  638.  
  639. /* Generate code to raise exception RAISE_ID.
  640.    If EXP is NULL_TREE, then PARMS is the list of parameters to use
  641.    for constructing this exception.
  642.    If EXP is non-NULL, then it is an already constructed object
  643.    of the kind that we want.  */
  644. void
  645. cplus_expand_raise (raise_id, parms, exp)
  646.      tree raise_id;
  647.      tree parms;
  648.      tree exp;
  649. {
  650.   /* Allocate new exception of appropriate type, passing
  651.      PARMS to its constructor.  */
  652.   tree cname, name;
  653.   tree decl;
  654.   tree xexp = exp;
  655.  
  656.   cname = lookup_exception_cname (current_class_type, current_class_name, raise_id);
  657.   if (cname == error_mark_node)
  658.     return;
  659.   name = TREE_VALUE (raise_id);
  660.  
  661.   decl = lookup_exception_object (cname, name, 1);
  662.   if (decl == NULL_TREE)
  663.     return;
  664.  
  665.   if (exp == NULL_TREE)
  666.     {
  667.       exp = build_method_call (NULL_TREE, name, parms, NULL_TREE, LOOKUP_COMPLAIN);
  668.       if (exp == error_mark_node)
  669.     return;
  670.     }
  671.  
  672.   if (in_try_block (1))
  673.     {
  674.       expand_raise (decl);
  675.     }
  676.   else if (! current_function_decl)
  677.     {
  678.       if (xexp == NULL_TREE)
  679.     error_with_decl (decl, "invalid raise of `%s' outside of functions");
  680.       else
  681.     error_with_decl (decl, "invalid reraise of `%s' outside of functions");
  682.     }
  683.   else
  684.     {
  685.       /* Test this raise against what this function permits.  */
  686.       tree names = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl));
  687.       while (names)
  688.     {
  689.       if (decl == TREE_TYPE (names))
  690.         break;
  691.       names = TREE_CHAIN (names);
  692.     }
  693.       if (names == NULL_TREE)
  694.     {
  695.       error ("current function not declared to raise exception `%s'",
  696.          IDENTIFIER_POINTER (name));
  697.       return;
  698.     }
  699.     }
  700.  
  701.   expand_assignment (EHS_parms, exp, 0, 0);
  702.  
  703.   /* Set the global exception handler stack's NAME field
  704.      to the `name' of this exception.  The global exception
  705.      handler stack is the container for the exception object
  706.      we just built.
  707.  
  708.      We go through a function call to make life easier when debugging.  */
  709. #if 0
  710.   expand_assignment (EHS_name, build_unary_op (ADDR_EXPR, decl, 0), 0, 0);
  711. #else
  712.   parms = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, EHS_name, 0),
  713.              build_tree_list (NULL_TREE,
  714.                       build_unary_op (ADDR_EXPR, decl, 0)));
  715.   expand_expr (build_function_call (BIR, parms));
  716. #endif
  717.  
  718.   /* Activate thrower.  If we are inside a TRY statement,
  719.      we can cheat and not do this, saving a longjmp.  */
  720.   if (in_try_block (1) == 0)
  721.     {
  722.       sets_exception_throw_decl = 1;
  723.       expand_assignment (exception_throw_decl, integer_one_node, 0, 0);
  724.     }
  725.  
  726.   if (xexp == NULL_TREE)
  727.     {    
  728.       /* Invoke destructors for current procedure or handler.  */
  729.       if (! expand_escape_except ())
  730.     compiler_error ("except nesting botch");
  731.       /* Throw via `longjmp'... Done as side-effect of goto.  */
  732.     }
  733.   /* If we were re-raising, just let this fall through.
  734.      At the end of the reraises, the call to `expand_goto'
  735.      will take care of everybody.  */
  736. }
  737.  
  738. tree
  739. cplus_expand_start_catch (raise_id)
  740.      tree raise_id;
  741. {
  742.   tree cname = lookup_exception_cname (current_class_type, current_class_name, raise_id);
  743.   tree decl;
  744.   tree ref, cond;
  745.  
  746.   if (cname == error_mark_node)
  747.     {
  748.       decl = error_mark_node;
  749.       cond = error_mark_node;
  750.     }
  751.   else
  752.     {
  753.       decl = lookup_exception_object (cname, TREE_VALUE (raise_id), 1);
  754.       if (decl == NULL_TREE)
  755.     {
  756.       cond = error_mark_node;
  757.     }
  758.       else
  759.     {
  760.       ref = build (COMPONENT_REF, ptr_type_node,
  761.                current_exception_decl, TREE_OPERAND (EHS_name, 1));
  762.       cond = build_binary_op (EQ_EXPR, build_unary_op (ADDR_EXPR, decl, 0), ref);
  763.     }
  764.     }
  765.   expand_start_cond (cond, 0);
  766.  
  767.   /* Does nothing right now.  */
  768.   expand_catch (decl);
  769.   if (current_exception_type
  770.       && TYPE_NEEDS_DESTRUCTOR (current_exception_type))
  771.     {
  772.       /* Make a cleanup for the name-specific exception object now in scope.  */
  773.       tree cleanup = maybe_build_cleanup (current_exception_object);
  774.       expand_start_bindings (0);
  775.       expand_decl_cleanup (NULL_TREE, cleanup);
  776.     }
  777.   return decl;
  778. }
  779.  
  780. void
  781. cplus_expand_end_catch (for_reraise)
  782.      int for_reraise;
  783. {
  784.   if (current_exception_type
  785.       && TYPE_NEEDS_DESTRUCTOR (current_exception_type))
  786.     {
  787.       /* Destroy the specific exception object now in scope.  */
  788.       expand_end_bindings (getdecls (), 0, 1);
  789.     }
  790.   if (for_reraise)
  791.     {
  792.       if (! expand_escape_except ())
  793.     abort ();
  794.     }
  795.   else
  796.     {
  797.       if (! expand_end_catch ())
  798.     abort ();
  799.     }
  800.   expand_end_cond ();
  801. }
  802.  
  803. /* Reraise an exception.
  804.    If EXCEPTIONS is NULL_TREE, it means reraise whatever exception was caught.
  805.    If EXCEPTIONS is an IDENTIFIER_NODE, it means reraise the exception
  806.    object named by EXCEPTIONS.  This must be a variable declared in
  807.    an `except' clause.
  808.    If EXCEPTIONS is a TREE_LIST, it is the list of exceptions we are
  809.    willing to reraise.  */
  810.  
  811. void
  812. cplus_expand_reraise (exceptions)
  813.      tree exceptions;
  814. {
  815.   tree ex_ptr;
  816.   tree ex_object = current_exception_object;
  817.  
  818.   if (exceptions && TREE_CODE (exceptions) == IDENTIFIER_NODE)
  819.     {
  820.       /* Don't get tripped up if its TREE_TYPE is `error_mark_node'.  */
  821.       ex_object = IDENTIFIER_LOCAL_VALUE (exceptions);
  822.       if (ex_object == NULL_TREE || TREE_CODE (ex_object) != INDIRECT_REF)
  823.     {
  824.       error ("`%s' is not an exception decl", IDENTIFIER_POINTER (exceptions));
  825.       return;
  826.     }
  827.       assert (TREE_CODE (TREE_OPERAND (ex_object, 0)) == VAR_DECL);
  828.       exceptions = NULL_TREE;
  829.     }
  830.  
  831.   /* reraise ALL, used by compiler.  */
  832.   if (exceptions == NULL_TREE)
  833.     {
  834.       /* Now treat reraise like catch/raise.  */
  835.       expand_catch (error_mark_node);
  836.       expand_raise (error_mark_node);
  837.       expand_assignment (EHS_name,
  838.              build (COMPONENT_REF, ptr_type_node,
  839.                 current_exception_decl, TREE_OPERAND (EHS_name, 1)), 0, 0);
  840.       expand_assignment (EHS_parms,
  841.              build (COMPONENT_REF, ptr_type_node,
  842.                 current_exception_decl, TREE_OPERAND (EHS_parms, 1)), 0, 0);
  843.       if (in_try_block (1) == 0)
  844.     {
  845.       sets_exception_throw_decl = 1;
  846.       expand_assignment (exception_throw_decl, integer_one_node, 0, 0);
  847.     }
  848.       /* Set to zero so that destructor will not be called.  */
  849.       expand_assignment (build1 (NOP_EXPR, ptr_type_node, TREE_OPERAND (ex_object, 0)),
  850.              integer_zero_node, 0, 0);
  851.       if (! expand_escape_except ())
  852.     abort ();
  853.       return;
  854.     }
  855.  
  856.   ex_ptr = build1 (NOP_EXPR, NULL_TREE, TREE_OPERAND (ex_object, 0));
  857.  
  858.   /* reraise from a list of exceptions.  */
  859.   while (exceptions)
  860.     {
  861.       tree type = lookup_exception_type (current_class_type, current_class_name,
  862.                      exceptions);
  863.       if (type == NULL_TREE)
  864.     {
  865.       error ("`%s' is not an exception type",
  866.          IDENTIFIER_POINTER (TREE_VALUE (exceptions)));
  867.       current_exception_type = NULL_TREE;
  868.       TREE_TYPE (ex_object) = error_mark_node;
  869.       TREE_TYPE (ex_ptr) = error_mark_node;
  870.     }
  871.       else
  872.     {
  873.       current_exception_type = type;
  874.       /* In-place union.  */
  875.       TREE_TYPE (ex_object) = type;
  876.       TREE_TYPE (ex_ptr) = TYPE_POINTER_TO (type);
  877.     }
  878.  
  879.       /* Now treat reraise like catch/raise.  */
  880.       cplus_expand_start_catch (exceptions);
  881.       cplus_expand_raise (exceptions, NULL_TREE, ex_ptr);
  882.       /* Set to zero so that destructor will not be called.  */
  883.       if (TREE_TYPE (ex_ptr) != error_mark_node)
  884.     expand_assignment (ex_ptr, integer_zero_node, 0, 0);
  885.       cplus_expand_end_catch (1);
  886.       exceptions = TREE_CHAIN (exceptions);
  887.     }
  888.   /* Don't propagate any unhandled exceptions.  */
  889.   expand_expr (call_to_unhandled_exception (), 0, VOIDmode, 0);
  890. }
  891.  
  892. void
  893. setup_exception_throw_decl ()
  894. {
  895.   tree call_to_longjmp, parms;
  896.  
  897.   int old = suspend_momentary ();
  898.  
  899.   exception_throw_decl = build_decl (VAR_DECL, get_identifier (THROW_NAME), integer_type_node);
  900.   pushdecl (exception_throw_decl);
  901.   parms = tree_cons (NULL_TREE, EHS_handler,
  902.              build_tree_list (0, integer_one_node));
  903.   call_to_longjmp = build_function_call (BILJ, parms);
  904.  
  905.   expand_decl (exception_throw_decl);
  906.   expand_decl_cleanup (exception_throw_decl,
  907.                build (COND_EXPR, void_type_node,
  908.                   exception_throw_decl,
  909.                   call_to_longjmp, integer_zero_node));
  910.   DECL_INITIAL (exception_throw_decl) = integer_zero_node;
  911.   sets_exception_throw_decl = 0;
  912.   resume_momentary (old);
  913. }
  914.  
  915. void
  916. init_exception_processing ()
  917. {
  918.   extern tree unhandled_exception_fndecl;
  919.   tree cname = get_identifier ("ExceptionHandler");
  920.   tree field, chain;
  921.   tree ctor, dtor;
  922.   tree jmp_buf_type = build_array_type (integer_type_node,
  923.                     build_index_type (build_int_2 (_JBLEN-1, 0)));
  924.   tree jmp_buf_arg_type = build_pointer_type (integer_type_node);
  925.  
  926.   tree parmtypes = hash_tree_chain (jmp_buf_arg_type, NULL_TREE);
  927.   tree setjmp_fndecl, longjmp_fndecl, raise_fndecl;
  928.  
  929.   EHS_type = xref_tag (record_type_node, cname, NULL_TREE);
  930.  
  931.   push_lang_context (lang_name_c);
  932.   setjmp_fndecl = define_function ("setjmp",
  933.                    build_function_type (integer_type_node,
  934.                             parmtypes),
  935.                    NOT_BUILT_IN, 0);
  936.   BISJ = default_conversion (setjmp_fndecl);
  937.   longjmp_fndecl = define_function ("longjmp",
  938.                     build_function_type (integer_type_node,
  939.                              hash_tree_chain (jmp_buf_arg_type,
  940.                                       hash_tree_chain (integer_type_node, NULL_TREE))),
  941.                     NOT_BUILT_IN, 0);
  942.   raise_fndecl = define_function ("__raise_exception",
  943.                   build_function_type (void_type_node,
  944.                                hash_tree_chain (ptr_type_node,
  945.                                     hash_tree_chain (build_pointer_type (ptr_type_node), NULL_TREE))));
  946.   BILJ = default_conversion (longjmp_fndecl);
  947.   BIR = default_conversion (raise_fndecl);
  948.   BIUE = default_conversion (unhandled_exception_fndecl);
  949.  
  950.   pop_lang_context ();
  951.  
  952.   /* finish_struct will pop this.  */
  953.   pushclass (EHS_type, 0);
  954.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("parms"), ptr_type_node);
  955.   chain = field;
  956.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
  957.                  build_pointer_type (default_function_type));
  958.   TREE_CHAIN (field) = chain;
  959.   chain = field;
  960.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("handler"), jmp_buf_type);
  961.   TREE_CHAIN (field) = chain;
  962.   chain = field;
  963.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("prev"),
  964.                  TYPE_POINTER_TO (EHS_type));
  965.   TREE_CHAIN (field) = chain;
  966.   chain = field;
  967.  
  968.   ctor = build_lang_decl (FUNCTION_DECL, cname,
  969.               build_cplus_method_type (EHS_type, TYPE_POINTER_TO (EHS_type), void_list_node));
  970.   DECL_CONSTRUCTOR_P (ctor) = 1;
  971.   TREE_STATIC (ctor) = 1;
  972.   TREE_PUBLIC (ctor) = 1;
  973.   grokclassfn (EHS_type, cname, ctor, NO_SPECIAL, 0, 0);
  974.   finish_decl (ctor, 0, 0);
  975.   TREE_CHAIN (ctor) = chain;
  976.   chain = ctor;
  977.   dtor = build_lang_decl (FUNCTION_DECL, cname,
  978.               build_cplus_method_type (EHS_type, TYPE_POINTER_TO (EHS_type), void_list_node));
  979.   TREE_STATIC (dtor) = 1;
  980.   TREE_PUBLIC (dtor) = 1;
  981.   grokclassfn (EHS_type, cname, dtor, DTOR_FLAG, 0, 0);
  982.   finish_decl (dtor, 0, 0);
  983.   TREE_CHAIN (dtor) = chain;
  984.   chain = dtor;
  985.   TYPE_HAS_CONSTRUCTOR (EHS_type) = 1;
  986.   TYPE_HAS_DESTRUCTOR (EHS_type) = 1;
  987.   finish_struct (EHS_type, temp_tree_cons (NULL_TREE, chain, NULL_TREE), 0, 0);
  988. }
  989.  
  990. void
  991. init_exception_processing_1 ()
  992. {
  993.   register tree EHS_id = get_identifier ("exceptionHandlerStack");
  994.  
  995.   EHS_decl = IDENTIFIER_GLOBAL_VALUE (EHS_id);
  996.  
  997.   /* If we have no other definition, default to library implementation.  */
  998.   if (EHS_decl == NULL_TREE)
  999.     {
  1000.       EHS_decl = build_decl (VAR_DECL, EHS_id, TYPE_POINTER_TO (EHS_type));
  1001.       /* If we don't push this, its definition, should it be encountered,
  1002.      will not be seen.  */
  1003.       EHS_decl = pushdecl (EHS_decl);
  1004.       TREE_EXTERNAL (EHS_decl) = 1;
  1005.       TREE_STATIC (EHS_decl) = 1;
  1006.       TREE_PUBLIC (EHS_decl) = 1;
  1007.       finish_decl (EHS_decl, 0, 0);
  1008.     }
  1009.   else if (TREE_CODE (EHS_decl) != VAR_DECL
  1010.        || TREE_TYPE (EHS_decl) != TYPE_POINTER_TO (EHS_type))
  1011.     fatal ("exception handling declarations conflict with compiler's internal model");
  1012.  
  1013.   if (EHS_prev == NULL_TREE)
  1014.     {
  1015.       register tree EHS_DECL = build1 (INDIRECT_REF, EHS_type, EHS_decl);
  1016.       EHS_prev = build_component_ref (EHS_DECL, get_identifier ("prev"), 0, 0);
  1017.       EHS_handler = build_component_ref (EHS_DECL, get_identifier ("handler"), 0, 0);
  1018.       EHS_parms = build_component_ref (EHS_DECL, get_identifier ("parms"), 0, 0);
  1019.       EHS_name = build_component_ref (EHS_DECL, get_identifier ("name"), 0, 0);
  1020.     }
  1021.  
  1022. #ifdef DBX_DEBUGGING_INFO
  1023.   if (use_gdb_dbx_extensions)
  1024.     dbxout_eh_init (EHS_type, EHS_decl);    
  1025. #endif
  1026. }
  1027.